package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> list = nursingPlanMapper.getAll();
        return list;
    }

    /**
     * 添加护理计划
     *
     * @param dto
     */
    @Transactional
    @Override
    public void add(NursingPlanDto dto) {
//        向计划表中插入数据
        NursingPlan bean = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlanMapper.insert(bean);
        Long planId = bean.getId();
//        向详细计划表中插入数据
        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        for (NursingProjectPlanDto projectPlan : projectPlans) {
            projectPlan.setPlanId(planId);
        }

        nursingPlanMapper.insertPlans(projectPlans);
    }

    /**
     * 分页查询护理计划
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> p=nursingPlanMapper.getByPage(name,status);
        PageResponse pr=new PageResponse(p);
        pr.setRecords(p.getResult());
        return pr;
    }

    @Override
    public NursingPlanVo getById(Long id) {
        return nursingPlanMapper.getById(id);
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        Long id = nursingPlanDto.getId();
        nursingProjectPlanMapper.deleteGetById(id);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //修改护理项目和护理计划的表
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        nursingPlanMapper.update(nursingPlan);
        nursingProjectPlanMapper.batchInsert(projectPlans);
    }

    @Override
    public void deleteById(Long id) {
        //删除计划表，
        nursingPlanMapper.deleteGetById(id);
        //删除第三方表
        nursingProjectPlanMapper.deleteGetById(id);
    }

    @Override
    public void startOrStop(Long id, Long status) {
        nursingPlanMapper.startOrStop(id,status);
    }
}
